package com.giventime.stormcoming.units;

import com.giventime.stormcoming.StormComing;
import com.giventime.stormcoming.utils.StormDungeonUtils.ContentType;
import com.giventime.stormcoming.utils.StormDungeonUtils.EnemyClass;

public class StormEnemy extends StormUnit {

	private EnemyClass enemyClass;
	
	public StormEnemy(String name, EnemyClass enemyClass, ContentType contentType) {
		super(name, contentType);
		this.enemyClass = enemyClass;
		hitPoints = enemyClass.getHealth()*10;
	}
	
	public void reset() {
		hitPoints = enemyClass.getHealth()*10;
		dead = false;
	}

	public EnemyClass getEnemyClass() {
		return enemyClass;
	}

	@Override
	public synchronized void performAttacks(StormUnit enemy) {
		try {
			for (Ability ability : enemyClass.getAbilities()) {
				StormComing.addMessage(getName()+" casts "+ability.getName());
				enemy.receiveDamage(calculateDamage(ability.getDamage(), ability.getCoefficient()), enemyClass.getAccuracy());
				wait(300);
			}
			wait(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int calculateDamage(int damage, double coefficient) {
		return (int) (damage+(enemyClass.getDamage()*coefficient));
	}
	
	@Override
	public int calculateResistances() {
		return (int) (enemyClass.getResistance()*0.3);
	}

	@Override
	public boolean evade(int accuracy) {
		int evade = (int) Math.round(Math.random()*enemyClass.getAvoidance())*(getLvl()/2);
		int toHit = (int) Math.round(Math.random()*accuracy);		
		return evade > toHit;
	}
	
}
